package com.yuandengta.yomall.product.controller;

import com.yuandengta.yomall.product.entity.CategoryEntity;
import com.yuandengta.yomall.product.service.CategoryService;
import com.yuandengta.yomall.product.vo.Catalog2VO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 首页
     *
     * @param model
     * @return 一级分类
     */
    @GetMapping({"/", "/index.html"})
    public String index(Model model) {
        List<CategoryEntity> categorys = categoryService.getLevelCategories();
        model.addAttribute("categories", categorys);
        return "index";
    }

    /**
     * 组装分类数据
     * 二级分类
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catalog2VO>> getCatalogJson() {
        Map<String, List<Catalog2VO>> catalogJson = categoryService.getCatalogJson();
        return catalogJson;
    }

}
